package com.qxzn.app.util;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Random;

public class MathUtil {

    public static final String ALL_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String LETTER_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String NUMBER_CHARS = "0123456789";
    public static final String NUMBER_CHAR = "123456789";

    public static String getRandom(int length, String charType) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(charType.charAt(random.nextInt(charType.length())));
        }
        return sb.toString();
    }

    public static Integer parseInteger(String num) {
        try {
            return Integer.parseInt(num);
        } catch (Exception e) {
        }
        return null;
    }

    public static int parseInt(String num) {
        try {
            return Integer.parseInt(num);
        } catch (Exception e) {
        }
        return 0;
    }

    public static long parseLong(String num) {
        try {
            return Long.parseLong(num);
        } catch (Exception e) {
        }
        return 0;
    }

    public static float parseFloat(String num) {
        try {
            return Float.parseFloat(num);
        } catch (Exception e) {
        }
        return 0;
    }

    public static Float parseFloat1(String num) {
        try {
            return Float.parseFloat(num);
        } catch (Exception e) {
        }
        return null;
    }

    public static double parseDouble(String num) {
        try {
            return Double.parseDouble(num);
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * 保留小数
     *
     * @param pattern  保留小数位数，如：0.00表示保留两位小数
     * @param strValue 数值
     * @return
     */
    public static String retainDecimal(String pattern, Object strValue) {
        try {
            return new DecimalFormat(pattern).format(parseDouble(strValue + ""));
        } catch (Exception e) {
        }
        return "";
    }

    public static double round(float num) {
        try {
            return Math.round(num);
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * 去除百分号
     *
     * @param num
     * @return
     */
    public static String replacePercent(String num) {
        return (num + "").replace("%", "");
    }

    /**
     * 对字符串进行由小到大排序
     *
     * @param str 需要排序的字符串数组
     */
    public static void strSort(List<String> str) {
        for (int i = 0; i < str.size(); i++) {
            for (int j = i + 1; j < str.size(); j++) {
                if (str.get(i).compareTo(str.get(j)) > 0) {
                    swap(str, i, j);
                }
            }
        }
    }

    /**
     * 交换两个元素的位置的方法
     *
     * @param str 需要交换元素的数组
     * @param i   索引i
     * @param j   索引j
     */
    private static void swap(List<String> str, int i, int j) {
        String t = str.get(i);
        str.set(i, str.get(j));
        str.set(j, t);
    }

    public static void main(String[] args) {
    }
}
